<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $uid;

    public function authenticate()
    {
        $idEncrypt = $this->username;
        $passEncrypt = md5($this->password);
        $userObj = User::model()->findByAttributes(array('username'=>$idEncrypt));
        
        if($userObj===null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if($passEncrypt !== $userObj->password)
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->uid=$userObj->id;
            $this->setState('id', $userObj->id);
            $this->setState('username', $userObj->username);
            $this->setState('groupid', $userObj->groupid);
            $this->setState('login', 1);

            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId()
    {
        return $this->uid;
    }

    public function getName()
    {
        return $this->username;
    }
}